package pl.uz.zgora.plan;

import java.io.IOException;
import java.util.HashSet;

import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class SelectSubgroupActivity extends Activity {
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.select_subgroup_view);
	    
	    final ProgressDialog dialog = HelperMethodsManager.getProgressDialog(this);
	    dialog.setCancelable(false);
	    final Bundle intentParams = getIntent().getExtras();
	    final Integer selectedGroupId = intentParams.getInt("idGrupy");
	    final String selectedGroupName = intentParams.getString("nazwaGrupy");
	    final Grupa grupa = new Grupa(selectedGroupId, selectedGroupName);
	    final SOAPManager soapManager = new SOAPManager(this);
	    final Button btnNext = (Button) findViewById(R.id.buttonNext);
	    final Spinner subgroupsSpinner = (Spinner) findViewById(R.id.spinnerSubgroups);
	    final Intent intent = new Intent(this, ClickTipViewActivity.class);
	    intent.putExtra("idGrupy", selectedGroupId);
		intent.putExtra("nazwaGrupy", selectedGroupName);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		
	    Thread convertToSQL = new Thread(new Runnable() {

	    	Plan plan = null;

	    	public void run() {
	    		try {
	    			final HashSet<String> subgroups = new HashSet<String>();
	    			plan = soapManager.downloadGroupTimeTable(grupa);
	    			for(Przedmiot p: plan.getPrzedmioty()) {
	    				if(!HelperMethodsManager.isEmpty(p.getPodgrupa())) {
	    					String nazwaPodgurpy = p.getPodgrupa();
	    					subgroups.add(nazwaPodgurpy);
	    				}
	    			}
	    			if(subgroups.size() == 0) {
	    				startActivity(intent);
						setResult(0);
						finish();
	    			} else {
	    				String[] subgroupsArray = new String[subgroups.size()];
	    				subgroups.toArray(subgroupsArray);
	    				final ArrayAdapter<String> adapter = new ArrayAdapter<String>(SelectSubgroupActivity.this, android.R.layout.simple_spinner_item, subgroupsArray);
	    				adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    				subgroupsSpinner.post(new Runnable() {

	    					public void run() {
	    						subgroupsSpinner.setAdapter(adapter);

	    						btnNext.setOnClickListener(new OnClickListener() {

	    							public void onClick(View v) {

	    								
	    								String selectedSubgroup = adapter.getItem(subgroupsSpinner.getSelectedItemPosition());
	    								// Zapisanie podgrupy do pliku konfiguracyjnego
	    								PrefrencesManager pm = PrefrencesManager.getInstance(v.getContext());
	    								pm.addStringToPreferences("podgrupa", selectedSubgroup);
	    								startActivity(intent);
	    								setResult(0);
	    								finish();
	    							}
	    						});	
	    						btnNext.setVisibility(View.VISIBLE);
	    					}
	    				});
	    			}
	    			dialog.dismiss();
	    		} catch (IOException e) {
	    			dialog.dismiss();
	    			HelperMethodsManager.manageException("SelectSubgroupActivity::onCreate()", e.getLocalizedMessage(), SelectSubgroupActivity.this);
	    		} catch (XmlPullParserException e) {
	    			dialog.dismiss();
	    			HelperMethodsManager.manageException("SelectSubgroupActivity::onCreate()", e.getLocalizedMessage(), SelectSubgroupActivity.this);
	    		}
	    	}
	    });
	    dialog.show();
	    convertToSQL.start();
	}
}
